package com.bj.disc.ui.login.utils;

import android.os.AsyncTask;

import com.ai3d.rfid.common.log.Logcat;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

public class UnZipAsyncTask extends AsyncTask<Void, Double, Void> {
    private static final String TAG = "UnZipAsyncTask";
    private String zipFileString;
    private String outPathString;
    private ZipProgressUtil.ZipListener listener;

    public UnZipAsyncTask(String zipFileString, String outPathString, ZipProgressUtil.ZipListener listener) {
        this.zipFileString = zipFileString;
        this.outPathString = outPathString;
        this.listener = listener;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        listener.zipStart();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        try {
            long sumLength = 0;
            long ziplength = getZipTrueSize(zipFileString);
            Logcat.d(TAG, "====文件的大小==" + ziplength);

            try (ZipFile zipFile = new ZipFile(zipFileString)) {
                Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
                byte[] buffer = new byte[8192]; // Larger buffer size for better performance

                while (entries.hasMoreElements()) {
                    ZipArchiveEntry entry = entries.nextElement();
                    String szName = entry.getName();
                    File file = new File(outPathString + File.separator + szName);

                    if (entry.isDirectory()) {
                        file.mkdirs();
                    } else {
                        file.createNewFile();
                        try (InputStream inputStream = zipFile.getInputStream(entry);
                             FileOutputStream out = new FileOutputStream(file)) {
                            int len;
                            while ((len = inputStream.read(buffer)) != -1) {
                                sumLength += len;
                                double progress = ((sumLength * 100.0) / ziplength);
                                if (progress - lastProgress >= 1.0) {
                                    publishProgress(progress);
                                    lastProgress = progress;
                                }
                                out.write(buffer, 0, len);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            cancel(true); // Notify that the task was cancelled due to an exception
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Double... values) {
        super.onProgressUpdate(values);
        listener.zipProgress(values[0]);
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        listener.zipSuccess();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        listener.zipFail();
    }

    double lastProgress = 0.0;

    private long getZipTrueSize(String filePath) {
        long size = 0;
        try (ZipFile zipFile = new ZipFile(filePath)) {
            Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
            while (entries.hasMoreElements()) {
                size += entries.nextElement().getSize();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return size;
    }
}

    // To start the unzipping task:

